/**
 * Created by Lcp on 2017/12/20.
 */
var h = $(window).height(),
    type = 0,
    afkssj = '',
    afjssj = '',//案发起止时间
    flag = true,//条件关闭开启判断
    node = '',//当前节点
    ajlxCode = [5,6,7,8,10],//案件类型code
    sendMsg = [],	//发送数据
    XH = [],		//唯一标识
    nums = 0 ;//请求条数
$(function(){
    $('.row-hoof').css({height: h, backgroundColor: '#d5dbe7',overflow: 'hidden'});
    $('.containers').css({height: h - 20 + 'px'});
    $('.noContent').css({height: h - 400 + 'px'});
    $('.sidebar').css({height: $('.containers').height()-$('.contentList').height()});
    $('.article').css({height: $('.containers').height()-$('.contentList').height()});
    $('.list').css({height: $('.containers').height() - $('.contentList').height() - 100});
    $('.case_name').css({height: $('.list').height()});
    Echarts();
    //日期
    laydate.render({
        elem: '#afsj',
        type: 'datetime',
        range: '~',
        theme: 'molv',//主题
        min: '1900-1-1 00:00:00',
        max: 1,
        format: 'yyyy-MM-dd HH:mm:ss',
        done: function(value, date, endDate){
            if(value != ''){
                afkssj = value.split('~')[0].split(' ')[0] + ' 00:00:00';
                afjssj = value.split('~')[1].split(' ')[1] + ' 23:59:59';
            }else{
                afkssj = '';
                afjssj = '';
            }
        }
    });
    $(window).resize(function(){
        h = $(window).height();
        $('.row-hoof').css({height: h, backgroundColor: '#d5dbe7',overflow: 'hidden'});
        $('.containers').css({height: h - 20 + 'px'});
        $('.noContent').css({height: h - 400 + 'px'});
        $('.sidebar').css({height: $('.containers').height()-$('.contentList').height()});
        $('.article').css({height: $('.containers').height()-$('.contentList').height()});
        $('.list').css({height: $('.containers').height() - $('.contentList').height() - 100});
     	$('.case_name').css({height: $('.list').height()});
        Echarts();
    })
})
function nofind(item){
    item.src = 'common/img/nothing.png';
    item.onerror = null;
}
function nulls(item){
    var callback;
    if(item != null && item != undefined){
        callback = item;
    } else {
        callback = '无';
    }
    return callback;
}

//二级弹出框
function listDetail(item){
	ajlxCode = [];
    $(item).toggleClass('Selected');
    if($(item).index() == 0){
        $(item).addClass('Selected');
        $(item).siblings().removeClass('Selected');
        ajlxCode = [5,6,7,8,10];
    }else{
        if($(item).parent().find('.Selected').length > 0 ){
            $(item).parent().find('span').eq(0).removeClass('Selected');
        }else{
            $(item).parent().find('span').eq(0).addClass('Selected');
            ajlxCode = [5,6,7,8,10];
        }
    };
    
	for(var i=0;i<$(item).parent().find('.Selected').length;i++){
		if($(item).parent().find('.Selected')[i].innerHTML == '盗窃案件'){
			ajlxCode.push(5)
		};
		if($(item).parent().find('.Selected')[i].innerHTML == '抢劫案件'){
			ajlxCode.push(6)
		}
		if($(item).parent().find('.Selected')[i].innerHTML == '抢夺案件'){
			ajlxCode.push(7)
		};
		if($(item).parent().find('.Selected')[i].innerHTML == '诈骗案件'){
			ajlxCode.push(8)
		}
		if($(item).parent().find('.Selected')[i].innerHTML == '八大案件'){
			ajlxCode.push(10)
		};
	}
}

//搜索
function judge(){
	nums  = Math.floor((5*$('.contentList').width()/6) / 160) * Math.floor($('.list').height()/ 210)
    $.ajax({
        type: 'get',
        url: baseUrls + 'casePhoto',
        data:{
        	beginTime:afkssj,
        	endTime:afjssj,
        	caseType:ajlxCode.toString(),
        	pageIndex: 1,
        	pageSize: nums
        },
        beforeSend: function(){
            Loading();
        },
        success: function(response){
            closeLoading();
            if(response.responseCode == '1000'){
                if(response.content.list.length > 0){
                    $('.content').css('display','block');
                    $('.list').html('');
                    $('.noContent').css({display: 'none'});
                    $('.total').html('为您找到相关结果<em>'+ response.content.total +'</em>条');
                    for(var i = 0; i < response.content.list.length; i++){
                    	var color = '';
                    	if(XH.toString().indexOf(response.content.list[i].XH)>= 0){
                    		color = 'rgb(255, 0, 0)';
                    	}else{
                    		color = 'rgb(51, 51, 51)';
                    	}
                    	var newLi = '<li><div class="img"><span class=fatherSpan><img src="'+nulls(response.content.list[i].WLLJ)+'" onerror="nofind(this)" ondblclick="openImg(this)"/></span></div><p class="ajmc">'+nulls(response.content.list[i].AJMC)+'</p><p class="text">'+nulls(response.content.list[i].NAME)+'</p><p class="operation"><span class="glyphicon glyphicon-heart-empty picture" style="color:'+color+'" onclick="listSelected(this)"><input type="hidden" class="src" value="'+ nulls(response.content.list[i].WLLJ) +'"/><input type="hidden" class="case_name" value="'+ nulls(response.content.list[i].AJMC) +'"/><input type="hidden" class="case_type" value="'+ nulls(response.content.list[i].NAME) +'"/><input type="hidden" class="case_id" value="'+ nulls(response.content.list[i].XH) +'"/></span></p></li>'
                    	$('.list').append(newLi)
                    }                   
                    $('#demo_pag1').bs_pagination({
                        totalPages: response.content.pages,
                        visiblePageLinks: 10,
                        currentPage: response.content.pageNum,
                        showGoToPage: false,
                        showRowsPerPage: false,
                        showRowsInfo: false,
                        showRowsDefaultInfo: false,
                        navListContainerClass: 'col-xs-12',
                        onChangePage: function (event, data) {
                            $.ajax({
                                type: 'get',
                                url: baseUrls + 'casePhoto',
                                data:{
						        	beginTime:afkssj,
						        	endTime:afjssj,
						        	caseType:ajlxCode.toString(),
						        	pageIndex: data.currentPage,
						        	pageSize: nums
						        },
                                beforeSend: function () {
                                    Loading();
                                },
                                success: function (response) {
                                     closeLoading();
                                	if(response.responseCode == '1000'){
                                		if(response.content.list.length > 0){
						                    $('.content').css('display','block');
						                    $('.list').html('');
						                    $('.noContent').css({display: 'none'});
						                    $('.total').html('为您找到相关结果<em>'+ response.content.total +'</em>条');
						                    for(var i = 0; i < response.content.list.length; i++){
						                    	var color = '';
						                    	if(XH.toString().indexOf(response.content.list[i].XH)>= 0){
						                    		color = 'rgb(255, 0, 0)';
						                    	}else{
						                    		color = 'rgb(51, 51, 51)';
						                    	}
						                    	var newLi = '<li><div class="img"><span><img src="'+nulls(response.content.list[i].WLLJ)+'" onerror="nofind(this)"/></span></div><p class="ajmc">'+nulls(response.content.list[i].AJMC)+'</p><p class="text">'+nulls(response.content.list[i].NAME)+'</p><p class="operation"><span class="glyphicon glyphicon-heart-empty picture" style="color:'+color+'" onclick="listSelected(this)"><input type="hidden" class="src" value="'+ nulls(response.content.list[i].WLLJ) +'"/><input type="hidden" class="case_name" value="'+ nulls(response.content.list[i].AJMC) +'"/><input type="hidden" class="case_type" value="'+ nulls(response.content.list[i].NAME) +'"/><input type="hidden" class="case_id" value="'+ nulls(response.content.list[i].XH) +'"/></span></p></li>'
						                    	$('.list').append(newLi)
						                    }   
						                }else{
						                    $('.content').css('display','none');
						                    $('.noContent').css({display: 'block'});
						                    Msg('未查到数据！');
						                }   
                                	} else{
						                $('.content').css('display','none');
						                $('.noContent').css({display: 'block'});
						                Msg();
						            }	
                                },
                                error: function () {
                                    closeLoading();
                                    $('.noContent').css({display: 'block'});
                                    Msg();
                                }
                            })
                        }
                    })
                    
                }else{
                    $('.content').css('display','none');
                    $('.noContent').css({display: 'block'});
                    Msg('未查到数据！');
                }
            } else{
                $('.content').css('display','none');
                $('.noContent').css({display: 'block'});
                Msg();
            }
        },
        error: function(){
            closeLoading();
            $('.noContent').css({display: 'block'});
            Msg();
        }
    })
}

//选中
function listSelected(item){
		if($(item).css('color')=='rgb(51, 51, 51)'){
//			if(sendMsg.length<6){
				$(item).css('color','red');
				sendMsg.push(
					{
						wllj: $(item).find('.src').val(),
						ajmc: $(item).find('.case_name').val(),
						ajlx: $(item).find('.case_type').val()
					}
				);
				XH.push($(item).find('.case_id').val());
				var newLi = '<li class="end">'+ nulls($(item).find('.case_name').val()) +'</li>';
				$('.sidebar ul').append(newLi);
//				shoppingCart();
//			}else{
//				Msg('您最多能选6张图片')
//			}
		}else{
			$(item).css('color','rgb(51, 51, 51)');
			var index = XH.indexOf($(item).find('.case_id').val());
			sendMsg.splice(index,1);
			XH.splice(index,1);
			$('.sidebar ul li').eq(index).remove();
		}
}

//发送数据
function sendInsert(item){
	console.log(sendMsg)
	if(sendMsg.length>3){
		$.ajax({
			type:'get',
			url: baseUrls + 'insertPhoto',
	        data:{
	        	allPhoto: JSON.stringify(sendMsg)
	        },
	        beforeSend: function () {
	            Loading();
	        },
	        success:function(response){
	        	 closeLoading();
	        	 if(response.responseCode == '1000'){
	        	 	Msg('发送成功！')
	        	 	$('.sidebar ul').empty();
	        	 	sendMsg = [];
	        	 	XH = [];
	        	 	$('.picture').css('color','rgb(51, 51, 51)')
	        	 }
	        },
			error: function () {
	            closeLoading();
	            $('.noContent').css({display: 'block'});
	            Msg();
	        }
		})
	}else{
		 Msg('请您至少选择4张图片！');
	}
	
}

//echarts
function Echarts(){
    var noContent = echarts.init(document.getElementById('noContent'));
    function createRandomItemStyle() {
        return {
            normal: {
                color: 'rgb(' + [
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160)
                ].join(',') + ')'
            }
        };
    }
    option = {
        tooltip: {
            show: false
        },
        series: [{
            name: '',
            type: 'wordCloud',
            size: ['80%', '80%'],
            textRotation : [0, 45, 90, -45],
            textPadding: 0,
            autoSize: {
                enable: true,
                minSize: 14
            },
            data: [
                {
                    name: "诈骗案",
                    value: 10000,
                    itemStyle: {
                        normal: {
                            color: 'black'
                        }
                    }
                },
                {
                    name: "相似度",
                    value: 6181,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "线索统计",
                    value: 4386,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "手动研判",
                    value: 4055,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "常住人口",
                    value: 2467,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "抢夺案",
                    value: 2244,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "其他",
                    value: 1898,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "简要案情",
                    value: 1484,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "相似案件",
                    value: 1112,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "火车站",
                    value: 965,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "相似案件",
                    value: 847,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "其他",
                    value: 582,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "抢劫案",
                    value: 555,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "抢盗",
                    value: 550,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "吸毒",
                    value: 462,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "强奸",
                    value: 366,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "诈骗",
                    value: 360,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "火车站",
                    value: 282,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "相似度",
                    value: 273,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "火车站",
                    value: 265,
                    itemStyle: createRandomItemStyle()
                }
            ]
        }]
    };
    noContent.setOption(option);
}
//打开
function openImg(item){
    layer.photos({
        photos: '.fatherSpan'
        ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });
}

